using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.VisualTree;
using Material.Icons;
using Material.Icons.Avalonia;

namespace DesktopApp.Views.Controls
{
    public partial class TitleBarControl : UserControl
    {
        public TitleBarControl()
        {
            InitializeComponent();
        }

        // --- Custom Title Bar Event Handlers (Moved from MainWindow.axaml.cs) ---

        // Helper to get the parent Window
        private Window? GetParentWindow()
        {
            return this.FindAncestorOfType<Window>();
        }

        private void TitleBar_PointerPressed(object? sender, PointerPressedEventArgs e)
        {
            // Check if the event source is an interactive control within the title bar
            // If it is, don't start the drag operation.
            bool isInteractiveControl = false;
            var source = e.Source as Control;
            while (source != null)
            {
                 // Check if the source or its ancestor is a Button, ComboBox, TextBox etc.
                 // Add other interactive types as needed.
                if (source is Button || source is ComboBox || source is TextBox)
                {
                    isInteractiveControl = true;
                    break;
                }
                // Stop searching if we reach the TitleBarControl itself
                if (source == this) 
                {
                    break;
                }
                source = source.Parent as Control;
            }

            // Only begin move drag if the source was not an interactive control
            if (!isInteractiveControl)
            {
                GetParentWindow()?.BeginMoveDrag(e);
            }
            // Do not set e.Handled = true here, allow event to propagate to controls like ComboBox
        }

        private void MinimizeButton_Click(object? sender, RoutedEventArgs e)
        {
            var window = GetParentWindow();
            if (window != null)
            {
                window.WindowState = WindowState.Minimized;
            }
        }

        private void MaximizeRestoreButton_Click(object? sender, RoutedEventArgs e)
        {
            var window = GetParentWindow();
            if (window != null)
            {
                window.WindowState = window.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
                
                // Update icon if the icon control exists within this UserControl
                var icon = this.FindControl<MaterialIcon>("MaximizeRestoreIcon");
                if (icon != null)
                {
                     icon.Kind = window.WindowState == WindowState.Maximized ? MaterialIconKind.WindowRestore : MaterialIconKind.WindowMaximize;
                }
            }
        }

        private void CloseButton_Click(object? sender, RoutedEventArgs e)
        {
            GetParentWindow()?.Close();
        }
    }
} 